/**
 * @author Ahammer
 * @date   2009 10
 * @update
 */

/*
 * This file is part of Iqm.
 * Copyright (c) 2010-2011 Helmut Ahammer
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package opgui;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.text.DecimalFormat;
import javax.media.jai.ParameterBlockJAI;
import javax.media.jai.PlanarImage;
import javax.swing.ButtonGroup;
import javax.swing.InputVerifier;
import javax.swing.JComponent;
import javax.swing.JFormattedTextField;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButtonMenuItem;
import javax.swing.JSpinner;
import javax.swing.JToolBar;
import javax.swing.SpinnerModel;
import javax.swing.SpinnerNumberModel;
import javax.swing.SwingUtilities;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import javax.swing.text.InternationalFormatter;

import main.IqmTools;
import main.Tank;


public class IqmFracIFSGUI  extends IqmOperatorJFrame
						  implements IqmOperatorGUIBuilder, ActionListener, PropertyChangeListener, ChangeListener {

	private  ParameterBlockJAI   pb         = null;  //  @jve:decl-index=0:
	//public  ImgProcFuncJFrame  opGUIFrame = null;
	//private  IqmFracIFSGUI 		 opGUIFrame = null;  //actionPerformed events werden in dieser Klasse abgehandelt
		

	private JPanel 				 	jPanelWidth		= null;	
	private JLabel				 	jLabelWidth		= null;
	private JSpinner			 	jSpinnerWidth 	= null;
	
	private JPanel 				 	jPanelHeight	= null;	
	private JLabel				 	jLabelHeight	= null;
	private JSpinner			 	jSpinnerHeight 	= null;

	private JPanel 				 	jPanelItMax  	= null;	
	private JLabel				 	jLabelItMax	    = null;
	private JSpinner			 	jSpinnerItMax 	= null;
	
	private JPanel 				 	jPanelNumPoly  	 = null;	
	private JLabel				 	jLabelNumPoly	 = null;
	private JSpinner			 	jSpinnerNumPoly  = null;
	
	private  JRadioButtonMenuItem  buttSierpinski1 = null;	
	private  JRadioButtonMenuItem  buttSierpinski2 = null;	
	private  JRadioButtonMenuItem  buttFern        = null;	
	private  JRadioButtonMenuItem  buttKochLine    = null;	
	private  JRadioButtonMenuItem  buttKochSnow    = null;	
	private  JRadioButtonMenuItem  buttMenger	   = null;		
	private  JRadioButtonMenuItem  buttHeighway	   = null;		
	private  JRadioButtonMenuItem  buttFracTypHidden   = null;
	private  JToolBar              jToolBarFracTyp     = null;
	private  ButtonGroup           buttGroupFracTyp    = null;
	
	/**
	 * constructor
	 */
	public IqmFracIFSGUI() {

	}

	
	/**
	 * This method sets all  variables to null
	 */
	private void resetVariables(){
		pb         = null; 
		//opGUIFrame = null; 
		
		//jToolBarRGB  = null;

	}
	/**
	 * This method sets all parameters to null
	 */
	private void resetParameters(){
		//butt       = null;
		//butt       = null;
	}

	/**
	 * This method sets the current parameter block to the argument
	 * @param ParameterBlockJAI pb
	 */
	@Override
	public void setParameterBlock(ParameterBlockJAI arg){
		pb = arg;
		IqmTools.setCurrParameterBlockJAI(pb);	
	}
	/**
	 * This method sets the current parameter block
	 * The individual values of the GUI  current ParameterBlock
	 *
	 */
	@Override
	public void setParameterBlock(){
		pb.setParameter("Width",   ((Number)jSpinnerWidth.getValue()).intValue());
		pb.setParameter("Height",  ((Number)jSpinnerHeight.getValue()).intValue());
		pb.setParameter("ItMax",   ((Number)jSpinnerItMax.getValue()).intValue());
		pb.setParameter("NumPoly", ((Number)jSpinnerNumPoly.getValue()).intValue());
	    if (buttSierpinski1.isSelected()) pb.setParameter("FracType", 0);
	    if (buttSierpinski2.isSelected()) pb.setParameter("FracType", 1);
		if (buttFern.isSelected())        pb.setParameter("FracType", 2);
		if (buttKochLine.isSelected())    pb.setParameter("FracType", 3);
		if (buttKochSnow.isSelected())    pb.setParameter("FracType", 4);
		if (buttMenger.isSelected())      pb.setParameter("FracType", 5);
		if (buttHeighway.isSelected())    pb.setParameter("FracType", 6);
		
		jSpinnerNumPoly.setEnabled(false);
		if (buttKochSnow.isSelected()) jSpinnerNumPoly.setEnabled(true); 
		
		IqmTools.setCurrParameterBlockJAI(pb);	
	}
	
	/**
	 * This method sets the current parameter values
	 *
	 */
	@Override
	public void setParameterValues(){
		
//	    //there is no need for a current image
//		int idx = IqmTools.getCurrManagerImgIndex();
//	    PlanarImage pi = Tank.getCurrentTankImageAt(idx); 
//	    if (pi == null){
//	    	System.out.println("IqmFracIFSGUI: Current image not defined");
//	    	return;
//	    }
        
	    jSpinnerWidth.removeChangeListener(this);	    	    
	    jSpinnerHeight.removeChangeListener(this);	    	    
	    jSpinnerItMax.removeChangeListener(this);	    	    
	    jSpinnerNumPoly.removeChangeListener(this);	    	    
	    jSpinnerWidth.setValue(pb.getIntParameter("Width"));
	    jSpinnerHeight.setValue(pb.getIntParameter("Height"));
	    jSpinnerItMax.setValue(pb.getIntParameter("ItMax"));
	    jSpinnerNumPoly.setValue(pb.getIntParameter("NumPoly"));
	    jSpinnerWidth.addChangeListener(this);
	    jSpinnerHeight.addChangeListener(this);
	    jSpinnerItMax.addChangeListener(this);
	    jSpinnerNumPoly.addChangeListener(this);
	    
		if (pb.getIntParameter("FracType") == 0) buttSierpinski1.setSelected(true);
		if (pb.getIntParameter("FracType") == 1) buttSierpinski2.setSelected(true);
		if (pb.getIntParameter("FracType") == 2) buttFern.setSelected(true);
		if (pb.getIntParameter("FracType") == 3) buttKochLine.setSelected(true);
		if (pb.getIntParameter("FracType") == 4) buttKochSnow.setSelected(true);
		if (pb.getIntParameter("FracType") == 5) buttMenger.setSelected(true);
		if (pb.getIntParameter("FracType") == 6) buttHeighway.setSelected(true);

		jSpinnerNumPoly.setEnabled(false);
		if (buttKochSnow.isSelected()) jSpinnerNumPoly.setEnabled(true); 

	}

	/**
	 * This method creates and shows the GUI
	 */
	@Override
	public void createIqmOperatorGUI() {
		//System.out.println("IqmFracIFS: createAndShowGUI invoked");
		//opGUIFrame = new IqmFracIFSGUI();      //actionPerforemd events werden in dieser Klasse abgehandelt
		//opGUIFrame.getJContentPane();
		this.initialize(); //Aufruf von ImgProcFuncJFrame
		this.setTitle("FracIFS");
		//There is no need for a current image
		//PlanarImage pi = Tank.getCurrentTankImageAt(IqmTools.getCurrManagerImgIndex());	
		
		//this.setSize(dim.width, dim.height+280);
		//this.setPreferredSize(new Dimension(800, 600));

		this.getJContentPane().add(getJPanelWidth(),     getGridBagConstraintsWidth());
		this.getJContentPane().add(getJPanelHeight(),    getGridBagConstraintsHeight());
		this.getJContentPane().add(getJPanelItMax(),     getGridBagConstraintsItMax());
		this.getJContentPane().add(getJPanelNumPoly(),   getGridBagConstraintsNumPoly());
		this.getJContentPane().add(getJToolBarFracTyp(), getGridBagConstraintsButtonFracTypGroup());
		this.pack();
		//this.repaint();
		if (pb == null) pb = new ParameterBlockJAI("IqmFracIFS");  //nur wenn das erste mal aufgerufen
		//pb = new ParameterBlockJAI("iqmthreshold"); //immer der default Block
		IqmTools.setCurrParameterBlockJAI(pb);
		this.setParameterValues();
	
	}
	
	private  GridBagConstraints getGridBagConstraintsWidth(){
		GridBagConstraints gridBagConstraintsWidth = new GridBagConstraints();
		gridBagConstraintsWidth.gridx = 1;
		gridBagConstraintsWidth.gridy = 5;
		gridBagConstraintsWidth.gridwidth = 1;//?
		gridBagConstraintsWidth.insets = new Insets(20,0,0,0);  //top left bottom right
		//gridBagConstraintsWidth.fill = GridBagConstraints.BOTH;
		return gridBagConstraintsWidth;
	}
	private  GridBagConstraints getGridBagConstraintsHeight(){
		GridBagConstraints gridBagConstraintsHeight = new GridBagConstraints();
		gridBagConstraintsHeight.gridx = 2;
		gridBagConstraintsHeight.gridy = 5;
		gridBagConstraintsHeight.gridwidth = 2;//?
		gridBagConstraintsHeight.insets = new Insets(20,0,0,0);  //top left bottom right
		//gridBagConstraintsHeight.fill = GridBagConstraints.BOTH;
		return gridBagConstraintsHeight;
	}
	private  GridBagConstraints getGridBagConstraintsItMax(){
		GridBagConstraints gridBagConstraintsItMax = new GridBagConstraints();
		gridBagConstraintsItMax.gridx = 1;
		gridBagConstraintsItMax.gridy = 6;
		gridBagConstraintsItMax.gridwidth = 1;//?
		gridBagConstraintsItMax.insets = new Insets(40,0,0,0);  //top left bottom right
		//gridBagConstraintsItMax.fill = GridBagConstraints.BOTH;
		return gridBagConstraintsItMax;
	}
	private  GridBagConstraints getGridBagConstraintsNumPoly(){
		GridBagConstraints gridBagConstraintsNumPoly = new GridBagConstraints();
		gridBagConstraintsNumPoly.gridx = 1;
		gridBagConstraintsNumPoly.gridy = 7;
		gridBagConstraintsNumPoly.gridwidth = 1;//?
		gridBagConstraintsNumPoly.insets = new Insets(2,0,0,0);  //top left bottom right
		//gridBagConstraintsNumPoly.fill = GridBagConstraints.BOTH;
		return gridBagConstraintsNumPoly;
	}
	private  GridBagConstraints getGridBagConstraintsButtonFracTypGroup(){
		GridBagConstraints gridBagConstraintsButtonFracTypGroup = new GridBagConstraints();
		gridBagConstraintsButtonFracTypGroup.gridx = 2;
		gridBagConstraintsButtonFracTypGroup.gridy = 6;
		gridBagConstraintsButtonFracTypGroup.gridwidth  = 2;//?
		gridBagConstraintsButtonFracTypGroup.gridheight = 2;//?
		gridBagConstraintsButtonFracTypGroup.insets = new Insets(10,0,20,0);  //top left bottom right		
		//gridBagConstraintsButtonFracTypGroup.fill = GridBagConstraints.BOTH;
		return gridBagConstraintsButtonFracTypGroup;
	}
	//------------------------------------------------------------------------------------------------------------------
	/**
	 * This method closes and destroys the GUI
	 * 
	 * @return void
	 */
//	public void destroyGUI() {
//		if (this != null){
//					
//			//this.closeAndDestroyGUI();
//			//resetVariables();
//			//folgendes geht auch!?
//			this.setVisible(false);
//			this.dispose();
//			//this = null;
//		}
//	}
	/**
	 * This method updates the GUI
	 * This method overrides IqmOperatorJFrame
	 */
	@Override
	public void updateIqmOperatorGUI(){

	}


	//-----------------------------------------------------------------------
	class IntNumberVerifier extends InputVerifier { //damit muss Eingabe richtig sein 
		@Override
		public boolean verify(JComponent input) {
		   JFormattedTextField ftf = (JFormattedTextField)input;
		   JFormattedTextField.AbstractFormatter formatter = ftf.getFormatter();
		   if (formatter != null) {
		      String text = ftf.getText();
		  	  try {
				   text = text.replace(",", ".");
				   Integer.valueOf(text);
				   //Float.valueOf(text);
					return true;
			  } catch (NumberFormatException e) {
					return false;
			  }	 
		   }
		   return true;
		}
//		public boolean shouldYieldFocus(JComponent input) {
//		System.out.println("NumberVerifier  shouldYieldFocus");
//
//          return verify(input);
//    }
}

	/**
	 * This method initializes jJPanelWidth	
	 * 	
	 * @return javax.swing.JPanel
	 */
	private JPanel getJPanelWidth() {
		if (jPanelWidth == null) {
			jPanelWidth = new JPanel();
			jPanelWidth.setLayout(new BorderLayout());		
			jLabelWidth = new JLabel("Width: ");
			jLabelWidth.setPreferredSize(new Dimension(70, 22));
			jLabelWidth.setHorizontalAlignment(JLabel.RIGHT);
			SpinnerModel sModel = new SpinnerNumberModel(512, 0, Integer.MAX_VALUE, 1); //init, min, max, step
			jSpinnerWidth = new JSpinner(sModel);
			jSpinnerWidth.setPreferredSize(new Dimension(60, 22));
			jSpinnerWidth.addChangeListener(this);
			JSpinner.DefaultEditor defEditor = (JSpinner.DefaultEditor)jSpinnerWidth.getEditor()  ;
			JFormattedTextField ftf = defEditor.getTextField() ;
			InternationalFormatter intFormatter = (InternationalFormatter)ftf.getFormatter() ;
			DecimalFormat decimalFormat = (DecimalFormat)intFormatter.getFormat() ;
			decimalFormat.applyPattern("#");  //decimalFormat.applyPattern("#,##0.0")  ;		
			jPanelWidth.add(jLabelWidth, 	 BorderLayout.WEST);
			jPanelWidth.add(jSpinnerWidth, BorderLayout.CENTER);			
		}
		return jPanelWidth;
	}
	/**
	 * This method initializes jJPanelHeight	
	 * 	
	 * @return javax.swing.JPanel
	 */
	private JPanel getJPanelHeight() {
		if (jPanelHeight == null) {
			jPanelHeight = new JPanel();
			jPanelHeight.setLayout(new BorderLayout());		
			jLabelHeight = new JLabel("Height: ");
			jLabelHeight.setHorizontalAlignment(JLabel.RIGHT);
			jLabelHeight.setPreferredSize(new Dimension(70, 22));
			SpinnerModel sModel = new SpinnerNumberModel(512, 0, Integer.MAX_VALUE, 1); //init, min, max, step
			jSpinnerHeight = new JSpinner(sModel);
			jSpinnerHeight.setPreferredSize(new Dimension(60, 22));
			jSpinnerHeight.addChangeListener(this);
			JSpinner.DefaultEditor defEditor = (JSpinner.DefaultEditor)jSpinnerHeight.getEditor()  ;
			JFormattedTextField ftf = defEditor.getTextField();
			InternationalFormatter intFormatter = (InternationalFormatter)ftf.getFormatter() ;
			DecimalFormat decimalFormat = (DecimalFormat)intFormatter.getFormat() ;
			decimalFormat.applyPattern("#");  //decimalFormat.applyPattern("#,##0.0")  ;		
			jPanelHeight.add(jLabelHeight, 	 BorderLayout.WEST);
			jPanelHeight.add(jSpinnerHeight, BorderLayout.CENTER);
			jSpinnerHeight.setEnabled(false);
		}
		return jPanelHeight;
	}
	
	/**
	 * This method initializes jJPanelItMax	
	 * 	
	 * @return javax.swing.JPanel
	 */
	private JPanel getJPanelItMax() {
		if (jPanelItMax == null) {
			jPanelItMax = new JPanel();
			jPanelItMax.setLayout(new BorderLayout());		
			jLabelItMax = new JLabel("ItMax: ");
			jLabelItMax.setHorizontalAlignment(JLabel.RIGHT);
			jLabelItMax.setPreferredSize(new Dimension(70, 22));
			SpinnerModel sModel = new SpinnerNumberModel(4, 0, Integer.MAX_VALUE, 1); //init, min, max, step
			jSpinnerItMax = new JSpinner(sModel);
			jSpinnerItMax.setPreferredSize(new Dimension(60, 22));
			jSpinnerItMax.addChangeListener(this);
			JSpinner.DefaultEditor defEditor = (JSpinner.DefaultEditor)jSpinnerItMax.getEditor()  ;
			JFormattedTextField ftf = defEditor.getTextField() ;
			InternationalFormatter intFormatter = (InternationalFormatter)ftf.getFormatter() ;
			DecimalFormat decimalFormat = (DecimalFormat)intFormatter.getFormat() ;
			decimalFormat.applyPattern("#");  //decimalFormat.applyPattern("#,##0.0")  ;		
			jPanelItMax.add(jLabelItMax, 	 BorderLayout.WEST);
			jPanelItMax.add(jSpinnerItMax, BorderLayout.CENTER);			
		}
		return jPanelItMax;
	}
	/**
	 * This method initializes jJPanelNumPoly	
	 * 	
	 * @return javax.swing.JPanel
	 */
	private JPanel getJPanelNumPoly() {
		if (jPanelNumPoly == null) {
			jPanelNumPoly = new JPanel();
			jPanelNumPoly.setLayout(new BorderLayout());		
			jLabelNumPoly = new JLabel("NumPoly: ");
			jLabelNumPoly.setHorizontalAlignment(JLabel.RIGHT);
			jLabelNumPoly.setPreferredSize(new Dimension(70, 22));
			SpinnerModel sModel = new SpinnerNumberModel(3, 3, Integer.MAX_VALUE, 1); //init, min, max, step
			jSpinnerNumPoly = new JSpinner(sModel);
			jSpinnerNumPoly.setPreferredSize(new Dimension(60, 22));
			jSpinnerNumPoly.addChangeListener(this);
			JSpinner.DefaultEditor defEditor = (JSpinner.DefaultEditor)jSpinnerNumPoly.getEditor()  ;
			JFormattedTextField ftf = defEditor.getTextField() ;
			InternationalFormatter intFormatter = (InternationalFormatter)ftf.getFormatter() ;
			DecimalFormat decimalFormat = (DecimalFormat)intFormatter.getFormat() ;
			decimalFormat.applyPattern("#");  //decimalFormat.applyPattern("#,##0.0")  ;		
			jPanelNumPoly.add(jLabelNumPoly, 	 BorderLayout.WEST);
			jPanelNumPoly.add(jSpinnerNumPoly, BorderLayout.CENTER);			
		}
		return jPanelNumPoly;
	}

	//--------------------------------------------------------------------------------------------
	/**
	 * This method initializes the Option: 
	 * 	
	 * @return javax.swing.JRadioButtonMenuItem	
	 */
	private  JRadioButtonMenuItem getJRadioButtonMenuButtSierpinski1() {
		if (buttSierpinski1 == null) {
			buttSierpinski1  = new JRadioButtonMenuItem();
			buttSierpinski1.setText("Sierpinski 1");
			buttSierpinski1.setToolTipText("generates a Sierpinski gasket by adding smaller triangles D=log(3)/log(2)=1.585");
			buttSierpinski1.addActionListener(this);
			buttSierpinski1.setActionCommand("parameter");
		}
		return buttSierpinski1;
	}
	/**
	 * This method initializes the Option: 
	 * 	
	 * @return javax.swing.JRadioButtonMenuItem	
	 */
	private  JRadioButtonMenuItem getJRadioButtonMenuButtSierpinski2() {
		if (buttSierpinski2 == null) {
			buttSierpinski2  = new JRadioButtonMenuItem();
			buttSierpinski2.setText("Sierpinski 2");
			buttSierpinski2.setToolTipText("generates a Sierpinski gasket (triangle) D=log(3)/log(2)=1.585");
			buttSierpinski2.addActionListener(this);
			buttSierpinski2.setActionCommand("parameter");
		}
		return buttSierpinski2;
	}

	/**
	 * This method initializes the Option: 
	 * 	
	 * @return javax.swing.JRadioButtonMenuItem	
	 */
	private  JRadioButtonMenuItem getJRadioButtonMenuButtFern() {
		if (buttFern == null) {
			buttFern  = new JRadioButtonMenuItem();
			buttFern .setText("Fern");
			buttFern .setToolTipText("generates a Fern");
			buttFern .addActionListener(this);
			buttFern.setActionCommand("parameter");
		}
		return buttFern;
	}
	/**
	 * This method initializes the Option: 
	 * 	
	 * @return javax.swing.JRadioButtonMenuItem	
	 */
	private  JRadioButtonMenuItem getJRadioButtonMenuButtKochLine() {
		//if (buttKochLine == null) {
			buttKochLine = new JRadioButtonMenuItem();
			buttKochLine.setText("Koch Line");
			buttKochLine.setToolTipText("generates a single Koch curev D=log(4)/log(3)=1.2619");
			buttKochLine.addActionListener(this);
			buttKochLine.setActionCommand("parameter");
		//}
		return buttKochLine;
	}
	/**
	 * This method initializes the Option: 
	 * 	
	 * @return javax.swing.JRadioButtonMenuItem	
	 */
	private  JRadioButtonMenuItem getJRadioButtonMenuButtKochSnow() {
		//if (buttKochSnow == null) {
			buttKochSnow = new JRadioButtonMenuItem();
			buttKochSnow.setText("Koch Snowflake");
			buttKochSnow.setToolTipText("generates a Koch Snowflake");
			buttKochSnow.addActionListener(this);
			buttKochSnow.setActionCommand("parameter");
		//}
		return buttKochSnow;
	}

	/**
	 * This method initializes the Option: 
	 * @return javax.swing.JRadioButtonMenuItem	
	 */
	private  JRadioButtonMenuItem getJRadioButtonMenuButtMenger() {
		//if (buttMenger == null) {
			buttMenger  = new JRadioButtonMenuItem();
			buttMenger .setText("Menger");
			buttMenger .setToolTipText("generates a Menger carpet (Sierpinski carpet) D=log(8)/log(3)=1.8928");
			buttMenger .addActionListener(this);
			buttMenger.setActionCommand("parameter");
		//}
		return buttMenger;
	}
	/**
	 * This method initializes the Option: 
	 * @return javax.swing.JRadioButtonMenuItem	
	 */
	private  JRadioButtonMenuItem getJRadioButtonMenuButtHeighway() {
		//if (buttHeighway == null) {
			buttHeighway  = new JRadioButtonMenuItem();
			buttHeighway .setText("Heighway Dragon");
			buttHeighway .setToolTipText("generates a Heighway dragon D=ln(2)/ln(sqrt(2))=2  Boundary: D=1.5236270862");
			buttHeighway .addActionListener(this);
			buttHeighway.setActionCommand("parameter");
		//}
		return buttHeighway;
	}
	/**
	 * This method initializes the hidden option	
	 * 	
	 * @return javax.swing.JRadioButtonMenuItem	
	 */
	private  JRadioButtonMenuItem getJRadioButtonMenuButtFracTypHidden() {
		//if (buttFracTypHidden == null) {
			buttFracTypHidden  = new JRadioButtonMenuItem();
			buttFracTypHidden .setText("Hidden");
			buttFracTypHidden.setVisible(false);
		//}
		return buttFracTypHidden;
	}
	/**
	 * This method initializes jJToolBarBar	
	 * 	
	 * @return javax.swing.JToolBar	
	 */
	private   JToolBar getJToolBarFracTyp() {
//		if (jToolBarFracTyp== null) {
			jToolBarFracTyp = new JToolBar("Options");
			jToolBarFracTyp.setOrientation(1); // 0 Horizontal 1 Vertical orientation
			jToolBarFracTyp.add(getJRadioButtonMenuButtSierpinski1());
			jToolBarFracTyp.add(getJRadioButtonMenuButtSierpinski2());
			jToolBarFracTyp.add(getJRadioButtonMenuButtFern());
			jToolBarFracTyp.add(getJRadioButtonMenuButtKochLine());
			jToolBarFracTyp.add(getJRadioButtonMenuButtKochSnow());
			jToolBarFracTyp.add(getJRadioButtonMenuButtMenger());
			jToolBarFracTyp.add(getJRadioButtonMenuButtHeighway());
			jToolBarFracTyp.add(getJRadioButtonMenuButtFracTypHidden());
			//jToolBarFracTyp.addSeparator();
			this.setButtonGroupFracTyp();	//Gruppieren der JRadioButtons
//		}
		return jToolBarFracTyp;
	}
	private  void setButtonGroupFracTyp() {
		//if (ButtonGroup buttGroup == null) {
			buttGroupFracTyp = new ButtonGroup();
			buttGroupFracTyp.add(buttSierpinski1);
			buttGroupFracTyp.add(buttSierpinski2);
			buttGroupFracTyp.add(buttFern);
			buttGroupFracTyp.add(buttKochLine);
			buttGroupFracTyp.add(buttKochSnow);
			buttGroupFracTyp.add(buttMenger);
			buttGroupFracTyp.add(buttHeighway);
			buttGroupFracTyp.add(buttFracTypHidden);
	}

	//--------------------------------------------------------------------------------------------
	public void actionPerformed(ActionEvent e) {
		//System.out.println("IqmFracIFSGUI: event e: " +e.getActionCommand());
		if ("reset".equals(e.getActionCommand())) {
			ParameterBlockJAI pb = new ParameterBlockJAI("IqmFracIFS".toLowerCase());
			IqmTools.setCurrParameterBlockJAI(pb);
			this.setParameterBlock(pb);
			this.setParameterValues();
			//this.updateIqmOperatorGUI();  //if necessary here or some lines above
			this.reset();
		}
		if ("parameter".equals(e.getActionCommand())) {
			int itMax  = ((Number)jSpinnerItMax.getValue()).intValue();
			if (buttSierpinski1 == e.getSource()) {
				if (itMax > 10) jSpinnerItMax.setValue(10);
			}
			if (buttSierpinski2 == e.getSource()) {
				if (itMax > 10) jSpinnerItMax.setValue(10);
			}
			if (buttFern == e.getSource()) {
				//if (itMax > 10) jSpinnerItMax.setValue(10);
			}
			if (buttKochLine == e.getSource()) {
				if (itMax > 5) jSpinnerItMax.setValue(5);
			}
			if (buttKochSnow == e.getSource()) {
				if (itMax > 10) jSpinnerItMax.setValue(10);
			}
			if (buttMenger == e.getSource()) {
				if (itMax > 10) jSpinnerItMax.setValue(10);
			}
			if (buttHeighway == e.getSource()) {
				if (itMax > 10) jSpinnerItMax.setValue(10);
			}						
			this.setParameterBlock();
			//this.updateIqmOperatorGUI();  //if necessary here or some lines above
		}
	
		//preview if selected
		if (this.isAutoPreviewSelected()){
			this.showPreview();
		}

	}
	//-----------------------------------------------------------------------------------------------

	@Override
	public void propertyChange(PropertyChangeEvent e) {
		int width  = ((Number)jSpinnerWidth.getValue()).intValue();
		int height = ((Number)jSpinnerHeight.getValue()).intValue();
		int itMax  = ((Number)jSpinnerItMax.getValue()).intValue();
		int NumPoly  = ((Number)jSpinnerNumPoly.getValue()).intValue();
	
		if (jSpinnerWidth  == e.getSource()) {
			jSpinnerHeight.setValue(width);
		}
		if (jSpinnerHeight  == e.getSource()) {
		}
		if (jSpinnerItMax  == e.getSource()) {
		}
		if (jSpinnerNumPoly  == e.getSource()) {
		}
		this.setParameterBlock();
		//this.updateIqmOperatorGUI();  //if necessary here or some lines above
		
		//preview if selected
		if (this.isAutoPreviewSelected()){
			this.showPreview();
		}
	}
	//-------------------------------------------------------------------------------------------------
	@Override
	public void stateChanged(ChangeEvent e) {
		
		int width    = ((Number)jSpinnerWidth.getValue()).intValue();
		int height   = ((Number)jSpinnerHeight.getValue()).intValue();
		int itMax    = ((Number)jSpinnerItMax.getValue()).intValue();
		int NumPoly  = ((Number)jSpinnerNumPoly.getValue()).intValue();
		


		if (jSpinnerWidth  == e.getSource()) {
			jSpinnerHeight.setValue(width);
		}
		if (jSpinnerHeight  == e.getSource()) {
		}
		if (jSpinnerItMax  == e.getSource()) {
		}
		if (jSpinnerNumPoly  == e.getSource()) {
		}
		this.setParameterBlock();
		//this.updateIqmOperatorGUI();  //if necessary here or some lines above
		
		//preview if selected
		if (this.isAutoPreviewSelected()){
			this.showPreview();
		}
		
	}


	//-------------------------------------------------------------------------------------------------
	public static void main() {

		SwingUtilities.invokeLater(new Runnable() {
			public void run() {
				IqmFracIFSGUI gui = new IqmFracIFSGUI();
				gui.createIqmOperatorGUI();

			}
		});
	}
	
}//END
